const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let target;
let sourceStr;
rl.on("line", (line) => {
    if (target === undefined) {
        [target, sourceStr] = line.trim().split(" ");
        console.log(getResult(target, sourceStr))
        target = sourceStr = undefined
    }
})

function getResult(target, sourceStr) {
    let n = target.length;
    let obj = {}
    for (const char of target) {
        if (!obj[char]) {
            obj[char] = 1
        } else {
            obj[char]++
        }
    }

    let left
    let right
    // 初始化
    for (let i = 0; i < n; i++) {
        let key = sourceStr[i]
        if (obj.hasOwnProperty(key)) {
            obj[key]--
        }
    }
    if (valid(obj)) {
        return 0
    }

    for (let i = 1; i < sourceStr.length - n + 1; i++) {
        left = i;
        right = left + n - 1;

        let leftKey = sourceStr[left - 1]
        let rightKey = sourceStr[right]

        if (obj.hasOwnProperty(leftKey)) {
            obj[leftKey]++
        }
        if (obj.hasOwnProperty(rightKey)) {
            obj[rightKey]--
        }
        if (valid(obj)) {
            return i
        }
    }
    return -1; // 不是关联子串
}

function valid(obj) {
    let flag = true
    for (const num of Object.values(obj)) {
        if (num !== 0) {
            flag = false
            break
        }
    }
    return flag
}